package com.lagou.spring_session.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 登录controller层
 */
@Controller
@RequestMapping("login")
public class LoginController {

    @RequestMapping("/toLogin")
    public String toLogin() {
        System.out.println("重定向到：登录页面");
        return "login";
    }

    @RequestMapping("/loginSystem")
    public String loginSystem(String username, String password, HttpSession httpSession) {

        if ("admin".equals(username) && "admin".equals(password)) {
            //登录成功,向session中存入用户信息
            System.out.println("=============登录成功=============");
            httpSession.setAttribute("username", username + System.currentTimeMillis());
            //重定向到登录成功页面
            return "redirect:/demo/success";
        }else {
            System.out.println("登录失败:用户名或密码错误");
            //重定向到登录页面
            return "redirect:/login/toLogin";
        }
    }
}
